//
//  GetItemBrain.m
//  tradersOnMap
//
//  Created by bilmuh bilmuh on 29.03.2011.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "GetItemBrain.h"
#import "GDataXMLNode.h"


@implementation GetItemBrain

@synthesize items;

-(void)getItems
{
	NSUserDefaults *def = [[NSUserDefaults alloc] init];	
	
    NSData* xmlData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://sorubank.ege.edu.tr/~b05067683/tez/getItems.php?userID=%@",[def objectForKey:@"userID"]]]];
	
    NSError* error;
    
    GDataXMLDocument* doc = [[GDataXMLDocument alloc] initWithData:xmlData options:0 error:&error];
	
    NSArray *itemNodes = [doc nodesForXPath:@"//items/item" error:nil];
    
    self.items = [[NSMutableArray alloc] init];
    
    for (GDataXMLNode *itemNode in itemNodes) {
        Item *item = [[Item alloc] init];
        
        item.itemID = ((GDataXMLElement*) [[itemNode nodesForXPath:@"itemID" error:nil] objectAtIndex:0]).stringValue;
        item.itemTitle = ((GDataXMLElement*) [[itemNode nodesForXPath:@"itemTitle" error:nil] objectAtIndex:0]).stringValue;
        item.description = ((GDataXMLElement*) [[itemNode nodesForXPath:@"description" error:nil] objectAtIndex:0]).stringValue; 
        item.imageURL = ((GDataXMLElement*) [[itemNode nodesForXPath:@"imageURL" error:nil] objectAtIndex:0]).stringValue;
        item.latitude = ((GDataXMLElement*) [[itemNode nodesForXPath:@"latitude" error:nil] objectAtIndex:0]).stringValue;
        item.longitude = ((GDataXMLElement*) [[itemNode nodesForXPath:@"longitude" error:nil] objectAtIndex:0]).stringValue;
		item.price = ((GDataXMLElement*) [[itemNode nodesForXPath:@"price" error:nil] objectAtIndex:0]).stringValue;
        
        [self.items addObject:item];
        
        [item release];
    }
    [doc release];
    [xmlData release];
}
@end
